package generateXML;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import myPDFParagraph.myParagraph;

public class XmlGenerator {

	public static void generateXmlByList(String outputPath, List<myParagraph> list) {
		try {
			Document doc = generateXml(list);
			outputXml(doc, outputPath);
		} catch (Exception e) {
			System.err.println("error");
		}
	}

	/**
	 *
	 * 
	 * @param doc
	 * @param fileName
	 * @throws Exception
	 */
	private static void outputXml(Document doc, String fileName)
			throws Exception {
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		DOMSource source = new DOMSource(doc);
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
		StreamResult result = new StreamResult(pw);
		transformer.transform(source, result);
		System.out.println("���XML�ļ��ɹ�!");
	}

	/**
	 *
	 * 
	 * @param list
	 * @return
	 */
	public static Document generateXml(List<myParagraph> list) {
		Document doc = null;
		Element root = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.newDocument();
			root = doc.createElement("PDF2PDF");
			doc.appendChild(root);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		int len = list.size();
		for (int i = 0; i < len; i++) {
			myParagraph paragraph = list.get(i);
			Element element = doc.createElement("paragraphs");
			element.setAttribute("ID", "" + paragraph.getID());
			element.setAttribute("Type", "" + paragraph.getType());
			element.setAttribute("IsDepend", "" +  paragraph.getIsDepend());
			element.setAttribute("Content", paragraph.getContent());
			if (paragraph.getSubList() != null && paragraph.getSubList().size() > 0) {
				for (int j = 0; j < paragraph.getSubList().size(); j ++) {
					Element subElement = doc.createElement("paragraph");
					myParagraph subParagraph = paragraph.getSubList().get(j);
					subElement.setAttribute("ID", "" + subParagraph.getID());
					subElement.setAttribute("Type", "" + subParagraph.getType());
					subElement.setAttribute("IsDepend", "" +  subParagraph.getIsDepend());
					subElement.setAttribute("Content", subParagraph.getContent());
					
					element.appendChild(subElement);
				}
			}
			root.appendChild(element);
		}
		return doc;
	}
}
